import {Component, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';

import {RulePointQuery} from './RulePointQuery';
import {RulePointVO} from './RulePointVO';
import {RuleVO} from '../../model/RuleVO';

@Component({
  selector: 'app-rulePointQuery',
  templateUrl: './rulePointQuery.component.html',
  styleUrls: ['./rulePointQuery.component.css'],
})
export class RulePointQueryComponent implements OnInit {
  beginDate: Date = new Date();
  endDate: Date = new Date();

  queryDate: Date[] = [];

  ranges = {Today: [new Date(), new Date()]};

  ruleVOS: RuleVO[] = [];

  selectRule: RuleVO = new RuleVO();

  nzFilterOption = () => true;

  rulePointVOs: RulePointVO[] = [];

  constructor(private httpClient: HttpClient) {
  }

  ngOnInit() {
    this.beginDate.setHours(0, 0, 0, 0);
    this.endDate.setHours(23, 59, 59, 999);
    this.queryDate = [this.beginDate, this.endDate];

    this.ranges.Today = [this.beginDate, this.endDate];

  }

  onRuleSerach(): void {
    let url = '/api/v1/stream/rule';
    let body: any = {};    
    this.httpClient.get(url, body).subscribe((data: any) => {
      this.ruleVOS = data;
    });
  }

  
  onSearch(): void {
  
    let rulePointQuery:RulePointQuery = new RulePointQuery();

    rulePointQuery.beginTime = this.queryDate[0].getTime();
    rulePointQuery.endTime = this.queryDate[1].getTime();
    rulePointQuery.ruleId = this.selectRule.id;

    this.httpClient
      .post<RulePointVO[]>('/api/v1/stream/ruleResult', rulePointQuery, {})
      .subscribe((results: RulePointVO[]) => {
        console.log(results);
        this.rulePointVOs = results;
        this.rulePointVOs.sort((v1,v2) => {
          return v2.eventTime - v1.eventTime;
        });
      });
  }
}
